package com.application.lucky.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.application.lucky.R;
import com.application.lucky.manager.UserManagers;
import com.application.lucky.request.CallBackEx;
import com.application.lucky.request.RedPRequest;
import com.application.lucky.widget.Keyboard;
import com.application.lucky.widget.PayEditText;
import com.application.lucky.widget.TitlebarView;
import com.application.mylibrary.base.BaseActivity;
import com.application.mylibrary.utils.ToastUtils;
import com.baselib.libnetwork.net.libnetworkcomponent.BaseResponse;

public class SetPayPawwordActivity extends BaseActivity {

    private TitlebarView mTitleBar;
    private PayEditText payEditText;
    private Keyboard keyboard;
    private static final String[] KEY = new String[]{
            "1", "2", "3",
            "4", "5", "6",
            "7", "8", "9",
            "", "0", "<<"
    };
    private TextView tvTip;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        setSubView();
        initEvent();
    }

    private void initEvent() {
        keyboard.setOnClickKeyboardListener(new Keyboard.OnClickKeyboardListener() {
            @Override
            public void onKeyClick(int position, String value) {
                if (position < 11 && position != 9) {
                    payEditText.add(value);
                } else if (position == 9) {

                } else if (position == 11) {
                    payEditText.remove();
                    //当点击完成的时候，也可以通过payEditText.getText()获取密码，此时不应该注册OnInputFinishedListener接口
//                    Toast.makeText(getApplication(), "您的密码是：" + payEditText.getText(), Toast.LENGTH_SHORT).show();
//                    finish();
                }
            }
        });

        /**
         * 当密码输入完成时的回调
         */
        payEditText.setOnInputFinishedListener(new PayEditText.OnInputFinishedListener() {
            @Override
            public void onInputFinished(String password) {

                if (isEdit) {//修改
                    if (UserManagers.getInstance().getPayPassword().equals(password)) {
                        payEditText.clearPw();
                        isEdit = false;
                        tvTip.setText("请输入新的密码！");
                    } else {
                        tvTip.setText("密码不正确，请重新输入！");
                        payEditText.clearPw();
                    }
                } else {//设置
                    if (TextUtils.isEmpty(password1)) {
                        password1 = password;
                        payEditText.clearPw();
                        tvTip.setText("请再次输入密码");
                    } else {
                        if (password.equals(password1)) {
                            setPayPW(password);
                        } else {
                            payEditText.clearPw();
                            password1 = null;
                            tvTip.setText("两次密码不一致，请重新设置！");
                        }
                    }
                }
            }
        });
    }

    private String password1;
    private boolean isEdit;

    private void setPayPW(final String password) {
        new RedPRequest().red_password_update(password, UserManagers.getInstance().getPayPassword(), new CallBackEx<BaseResponse<String>>() {
            @Override
            public void onSuccess(BaseResponse<String> stringBaseResponse) {
                UserManagers.getInstance().setPayPassword(password);
                finish();
                ToastUtils.show("设置成功");
            }

            @Override
            public void onFailed(int code) {
                ToastUtils.show("设置错误");
                isEdit = UserManagers.getInstance().isSetPayPW();
                tvTip.setText("请输入6位密码");
                password1 = null;
                payEditText.clearPw();

            }
        });
    }

    private void setSubView() {
        //设置键盘
        keyboard.setKeyboardKeys(KEY);
    }

    @Override
    public void beforeInit() {

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_set_pay_pawword;
    }

    private void initView() {

        mTitleBar = (TitlebarView) findViewById(R.id.title_bar);
        mTitleBar.setOnViewClick(new TitlebarView.onViewClick() {
            @Override
            public void leftClick() {
                finishAnim();
            }

            @Override
            public void rightClick() {

            }
        });
        payEditText = (PayEditText) findViewById(R.id.PayEditText_pay);
        keyboard = (Keyboard) findViewById(R.id.KeyboardView_pay);
        tvTip = findViewById(R.id.tv_set_tip);
        if (UserManagers.getInstance().isSetPayPW()) {
            isEdit = true;
            mTitleBar.setTitle("修改支付密码");
        } else {
            isEdit = false;
            mTitleBar.setTitle("设置支付密码");
        }
    }
}
